﻿// domain.cpp : 定义应用程序的入口点。
//


#include "domain.h"
#include "iocpSrv.h"
// Windows 头文件
#include <windows.h>
// C 运行时头文件
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include "../utils/command/CommandLine.h"

#include "header_def.h"
#include "SafeBoxInterface.h"

SAFEBOX_INFO gBoxInfo = { 0 };
IClassFactory* gClassFactory = NULL;
ISafeBoxInterface* gSafeBoxInterface = NULL;

BOOL InitSafeBoxEnvironment(UINT uBoxId, LPCWSTR strBoxName)
{
    BOOL IsBit32 = FALSE;
    HANDLE hProcess = GetCurrentProcess();
    WCHAR strTemp[MAX_PATH] = { 0 };
    HMODULE hModule = NULL;
    GetModuleFileNameW(NULL, strTemp, MAX_PATH);
    LPWSTR(wcsrchr(strTemp, L'\\'))[1] = L'\0';

    if (-1 == uBoxId || NULL == strBoxName || 0 == strBoxName[0])
    {
        return FALSE;
    }

    if (FALSE == IsWow64Process(hProcess, &IsBit32))
    {
        return FALSE;
    }

    wsprintfA(gBoxInfo.strSuffixA, "B%03X", uBoxId);
    wsprintfW(gBoxInfo.strSuffixW, L"B%03X", uBoxId);

    gBoxInfo.dwDoaminPid = GetCurrentProcessId();
    gBoxInfo.uBoxId = uBoxId;
    wcscpy_s(gBoxInfo.strName, strBoxName);

    if (IsBit32)
    {
        wcscat_s(strTemp, MAX_PATH, L"SafeBox_x86.dll");
    }
    else
    {
        wcscat_s(strTemp, MAX_PATH, L"SafeBox_x64.dll");
    }

    hModule = LoadLibraryW(strTemp);

    if (NULL == hModule)
    {
        return FALSE;
    }

    PFN_DllGetClassObject DllGetClassObject =
        (PFN_DllGetClassObject)GetProcAddress(hModule, "DllGetClassObject");

    if (NULL == DllGetClassObject)
    {
        return FALSE;
    }

    DllGetClassObject(IID_IUnknown, IID_IClassFactory, (void**)&gClassFactory);

    if (NULL == gClassFactory)
    {
        return FALSE;
    }

    gClassFactory->CreateInstance(NULL, __uuidof(ISafeBoxInterface), (void**)&gSafeBoxInterface);

    if (NULL == gSafeBoxInterface)
    {
        gClassFactory->Release();
        gClassFactory = NULL;
        return FALSE;
    }

    gSafeBoxInterface->SetEnvironment(&gBoxInfo);

    return TRUE;
}


#if 0
int _tmain(int argc, TCHAR* argv[])

#else
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)
#endif
{

    UINT uBoxId = -1;
    std::wstring strValue;
    CommandLineObj.Split('=');
    CommandLineObj.Find(L"boxId", strValue);

    if (strValue.size())
    {
        uBoxId = _wtoi(strValue.c_str());
    }
    CommandLineObj.Find(L"boxName", strValue);

    //while (FALSE == IsDebuggerPresent())
    //{
    //    Sleep(1000 * 3);
    //}
    
    if (FALSE == InitSafeBoxEnvironment(uBoxId, strValue.c_str()))
    {
        return FALSE;
    }
    else
    {
        CIocpSrv Service;
        Service.InitEnv();
        Service.Dispatch();
        Service.UninitEnv();
    }

    if (gSafeBoxInterface)
    {
        gSafeBoxInterface->Release();
        gSafeBoxInterface = NULL;
    }

    if (gClassFactory)
    {
        gClassFactory->Release();
        gClassFactory = NULL;
    }

    return 0;
}



